/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.suai.system.impl;

import java.util.LinkedList;
import java.util.List;
import org.suai.system.PackageGenerator;
import org.suai.system.QueueSystem;

/**
 *
 * @author awm
 */
public class PackageGeneratorRing implements PackageGenerator {
    private QueueSystem[] systems;

    @Override
    public void initialSystems(List systems) {
        //this.systems = (QueueSystem[])systems.toArray();
        this.systems = new QueueSystem[systems.size()];
        int i = 0;
        for(Object system : systems.toArray()) {
            this.systems[i++] = (QueueSystem)system;
        }
    }

    @Override
    public void generatePackages() {
        int mod = systems.length;

        for (int i = 0; i < systems.length; i++) {
            systems[i].generatePackages();
        }
        
        for(int i = 0; i < systems.length; i++) {
            List listPackage = new LinkedList();

            listPackage.addAll( systems[ ( (i == 0)?(mod - 1):( (i - 1) % mod) ) ].getPackagesForService() );
            listPackage.addAll( systems[ (i % mod) ].getPackagesForService() );
            listPackage.addAll( systems[ ( (i + 1) % mod) ].getPackagesForService() );

            systems[i].setOuterPackages(listPackage);
            //systems[i].servicePackages();
        }

    }

    @Override
    public void servicePackages() {
        for(int i = 0; i < systems.length; i++) {
            systems[i].servicePackages();
        }
    }

}
